from stock.trend_calculate import check_stock_conditions
import pandas as pd

def find_all_fall(df):
    # 计算每日的跌幅百分比
    df['daily_drop'] = (df['close'] - df['preclose']) / df['preclose'] * 100

    # 标记阴线日（收盘价低于开盘价）
    df['is_negative'] = (df['close'] < df['open'])
    # 找到满足条件的4个交易日范围
    satisfying_ranges = []
    for i in range(len(df) - 3):
        subset = df.iloc[i:i + 4]
        # 检查是否有3个交易日是阴线
        if (subset['is_negative'].sum() >= 3) and (subset['daily_drop'].sum() < -10) and (
                abs(subset['open'] - subset['close']).sum() >= subset['daily_drop'].sum() * 0.9):
            satisfying_ranges.append([subset['code'].iloc[0],str(subset['date'].iloc[0]) + " " + str(subset['date'].iloc[3])])
    return satisfying_ranges


if __name__=="__main__":
    from stock.stock_base_daily import daily_directory
    import os

    # 获取目录中的所有文件
    files = os.listdir(daily_directory)
    _rs_a =[]
    # 如果目录为空，直接返回
    if not files:
        print(f"目录 {daily_directory} 下没有文件.")
    # 遍历目录中的所有文件
    # 创建一个空的DataFrame
    rs = pd.DataFrame()
    for filename in files:
        # 拼接文件的完整路径
        file_path = os.path.join(daily_directory, filename)
        # 从CSV文件加载数据
        df = pd.read_csv(file_path, parse_dates=['date'])
        # 按日期排序
        df.sort_values('date', inplace=True)
        rs_stock = find_all_fall(df)
        if rs_stock:
            _rs_a.append(rs_stock)
    print(_rs_a)





